{% extends 'AdminBundle::layout.html.twig' %}


{% block title %}banner管理{% endblock %}

    {% block css %}
        <link type="text/css" rel="stylesheet" href="{{ asset('assets/css/bootstrap-datetimepicker.min.css') }}">
    {% endblock %}


    {% block top %}
        {#{{ dump() }}#}
        {{ parent() }}
    {% endblock %}



    {% block left_body_top %}
        {{ parent() }}
    {% endblock %}

    {% block left_body %}
        {{ parent() }}
    {% endblock %}

    {% block right_body %}
    <div class="page-header">
        <div class="page-right-top">
            <div class="right-top">
                <span class="right-title">系统设置</span>
            </div>
            <div class="top-nav">Banner图管理</div>
            <span class="r-arrow top-r-arrow"></span>
            <div class="top-nav top-nav-active">添加新Banner</div>
        </div>
        <div class="main-content">
            <div class="mes-content">

                <div class="main-mes-content">
                    <form id="comMesBtn">
                        <div class="com-list">

                            <div class="display">
                                <div class="com-list-box fl">
                                    <div class="com-left fl">Banner分类</div>
                                    <div class="select-jurisdiction fl">
                                        <span class="radio-icon {% if data.id >0 and data.is_company == 1 or data.id == 0  %}radio-icon-check{% endif %} fl" data-value="1"></span>
                                        <span class="file-text fl">企业端</span>
                                    </div>

                                    <div class="select-jurisdiction fl">
                                        <span class="radio-icon fl {% if data.id >0 and data.is_company == 0 %}radio-icon-check{% endif %}" data-value="0"></span>
                                        <span class="file-text fl">管理员端</span>
                                    </div>
                                </div>
                                <div class="clearfix"></div>
                            </div>

                            <div class="com-list-box">
                                <div class="com-left fl">Banner名称</div>
                                <input type="text" name="name" {% if data.id >0 %} value="{{ data.name }}"{% endif %} class="pop-com-input banner-name" style="width: 560px;" placeholder="请输入Banner名称">
                            </div>

                            <div class="com-list-box">
                                <div class="com-left fl">Banner链接</div>
                                <input type="text" name="url" {% if data.id >0 %} value="{{ data.url }}"{% endif %} class="pop-com-input banner-url" style="width: 560px;" placeholder="请输入Banner链接" >
                            </div>

                            <div class="com-list-box">
                                <div class="com-left fl">Banner图片</div>
                                <img style="min-height: 172px;" src="{{ asset(data.image_path |default('assets/img/icon/banner-defaults.png')) }}" width="400" height="172" class="fl new-path">

                                <div class="banner-explain fl">
                                    <label class="upload-btn banner-btn" for="upload_file">点击上传</label>

                                    <input type="file" id="upload_file">

                                    <div class="banner-text">建议图片尺寸750*322 请上传2M以内图片</div>
                                </div>

                                <div class="clearfix"></div>
                            </div>

                            <div class="clearfix"></div>
                            <div class="add-mem-box" style="margin-top: 40px;">
                                <div class="com-left fl"></div>
                                <div class="acc-opera-btn fl mem-btn p-first-btn">保存</div>
                                <a href="{{ path('setting_banner_list') }}" class="acc-opera-btn fl mem-cancel">取消</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {% endblock %}

{% block box %}
{% endblock %}


{% block javascript %}
<script type="text/javascript">
    $(function () {
        SidebarHighlighting('8');

        SonSidebar('8', '5');
        // Banner分类
        $('.radio-icon').on('click', function () {

            {% if data.id > 0 %}

                popStatus(2, '不能编辑banner分类', 1,'', true);

            {% else %}

                $('.radio-icon').removeClass('radio-icon-check');
                $(this).addClass('radio-icon-check');

            {% endif %}

        });

        /// ajax 上传图片

        var banner_path = "{{ data.image_path|default('') }}";
        function uploadFile() {
            var _file = this;
            var data = new FormData();
            data.append('image', _file.files[0]);

            URL_fileShareUpload = "{{ path('api_public_upload_image') }}";
            $.ajax({
                url:URL_fileShareUpload,
                type: 'POST',
                dataType: 'json',
                processData: false,
                contentType: false,
                data: data,
                success: function (data) {
                    if(data.errorCode == 0){

                        banner_path = data.data.path;
                        $(".new-path").attr("src","{{ asset('') }}"+banner_path);
                        popStatus(1, '上传成功', 1,'', true);
                    } else {
                        alert(data.message);
                        return false;
                    }
                }
            });
        }
        $("#upload_file").on("change", uploadFile);


        //保存
        $(".p-first-btn").click(function () {
            var prame = $("#comMesBtn").serializeObject();

            console.log(prame);
            if(prame.name == ""){
                popStatus(2, '请输入banner名称', 1,'', true);
                return;
            }

            if(prame.url.length == 7){
                prame.url = "";
            }


            prame.image = banner_path;

            if(prame.image == ""){
                popStatus(2, '请上传banner图片', 1,'', true);
                return;
            }

            {% if data.id == 0 %}
                prame.is_company = $(".radio-icon-check").data("value");
            {% endif %}

            prame.id = "{{ data.id }}";

            prame.role = "2";

            $.post("{{ path('api_admin_setting_edit_banner') }}",prame,function(data){
                console.log(data);
                if(data.errorCode == 0){
                    popStatus(1, data.message, 1,"{{ path('setting_banner_list') }}", true);
                } else if(data.errorCode == -1) {
                    popStatus(2, data.message, 1,"{{ path('auth_login') }}", true);
                } else {
                    popStatus(2, data.message, 1,'', true);
                }
            })


        })

    })
</script>
{% endblock %}